//
//  ZHToolBar.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/6.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class ZHToolBar: NSView {
    
    var customButtonFrame:NSRect = NSRect.zero {
        didSet {
            self.buttonView.frame = customButtonFrame
        }
    }
    
    var canClose:Bool = true {
        didSet {
            self.buttonView.closeButton.isHidden = !canClose
            self.buttonView.resetButtonPosition()
        }
    }
    var canMiniaturize:Bool = true {
        didSet {
            self.buttonView.minButton.isHidden = !canMiniaturize
            self.buttonView.resetButtonPosition()
        }
    }
    
    var canZoom:Bool = true {
        didSet {
            self.buttonView.maxButton.isHidden = !canZoom
            self.buttonView.resetButtonPosition()
        }
    }
    var canMove:Bool = true {
        didSet {
            self.movableView.isHidden = !canMove
        }
    }
    lazy var title: NSLabel = {
        let lab = NSLabel.label(title: "标题", textColor: color_gray(0), font: NSFont.systemFont(ofSize: 14))
        lab.alignment = .center
        lab.sizeToFit()
        return lab
    }()
    
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.commonInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func commonInitialize() {
        self.addSubview(movableView)
        self.addSubview(buttonView)
        
        self.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.height.equalTo(24)
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview()
        }
    }
    
    lazy var movableView: ZHMovableBackgroundView = {
        let view = ZHMovableBackgroundView.init(frame: self.bounds)
        return view
    }()
    lazy var buttonView: ZHWindowButtonView = {
        let btn = ZHWindowButtonView.init(frame: NSRect.init(x: 0, y: 0, width: 74, height: 16))
        return btn
    }()
    
    /// 添加事件
    public func newTarget(target:AnyObject?,forCloseButton action:Selector?) {
        self.buttonView.closeButton.target = target
        self.buttonView.closeButton.action = action
    }
    
    public func newTarget(target:AnyObject?,forMinButton action:Selector?) {
        self.buttonView.minButton.target = target
        self.buttonView.minButton.action = action
    }
    
    public func newTarget(target:AnyObject?,forMaxButton action:Selector?) {
        self.buttonView.maxButton.target = target
        self.buttonView.maxButton.action = action
    }
    
}
